package com.example.service.impl;

import com.example.entity.Household;
import com.example.entity.Staffs;
import com.example.mapper.HouseholdMapper;
import com.example.mapper.LoginMapper;
import com.example.result.CommonResult;
import com.example.service.LoginService;
import com.example.utils.Md5Utils;
import com.example.utils.RandomNameUtitls;
import com.example.utils.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @project: smart-property
 * @description: 登录 service 实现层
 * @author: cjq
 * @create: 2022-11-14 08:54
 **/
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    LoginMapper loginMapper;

    @Autowired
    HouseholdMapper householdMapper;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    JavaMailSender javaMailSender;

    public static String codes;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 后台 用户密码 登录
     * @param staffs
     * @return
     */
    @Override
    public CommonResult login(Staffs staffs) {
        String staffNumber = staffs.getStaffNumber();
        Staffs s = loginMapper.listStaffNumber(staffNumber);
        if (!StringUtils.isEmpty(s)){
            staffs.setPassword(Md5Utils.getMd5(staffs.getPassword()));
            Staffs login = loginMapper.login(staffs);
            if (!StringUtils.isEmpty(login)){
                return CommonResult.ok(login);
            }else {
                return CommonResult.fail("账户或密码有误!");
            }
        }else {
            return CommonResult.fail("账户不存在，请先进行注册!");
        }
    }

    /**
     * 后台 新用户注册
     * @param staffs
     * @return
     */
    @Override
    public int register(Staffs staffs) {
        staffs.setStaffName(RandomNameUtitls.getRandomJianHan(6));
        staffs.setPassword(Md5Utils.getMd5(staffs.getPassword()));
        staffs.setStaffPhone(staffs.getStaffNumber());
        staffs.setStatus(0);
        staffs.setCreateTime(new Date());
        return loginMapper.register(staffs);
    }

    /**
     * 查询账户是否有重复的
     * @param staffNumber
     * @return
     */
    @Override
    public CommonResult listStaffNumber(String staffNumber) {
        Staffs staffs = loginMapper.listStaffNumber(staffNumber);
        if (StringUtils.isEmpty(staffs)){
            return CommonResult.ok();
        }
        return CommonResult.fail("账户已存在！");
    }

    /**
     * app端账号密码登录
     * @param household
     * @return
     */
    @Override
    public CommonResult appLogin(Household household) {
        if (StringUtils.isEmpty(household.getPassword()) && StringUtils.isEmpty(household.getAccountNumber())){
            return CommonResult.fail("请输入账户/密码!");
        }
        if (StringUtils.isEmpty(household.getPassword())){
            return CommonResult.fail("请输入密码!");
        }
        if (!StringUtils.isEmpty(household.getAccountNumber())){
            if (!StringUtils.isEmpty(loginMapper.listAccountNumber(household.getAccountNumber()))){
                household.setPassword(Md5Utils.getMd5(household.getPassword()));
                Household household1 = loginMapper.appLogin(household);
                if (!StringUtils.isEmpty(household1)){
                    return CommonResult.ok(household1);
                }else {
                    return CommonResult.fail("账户或密码错误!");
                }
            }else {
                return CommonResult.fail("账户不存在，请进行注册!");
            }
        }else {
            return CommonResult.fail("请输入账户!");
        }
    }

    /**
     * app端用户注册
     * @param household
     * @return
     */
    @Override
    public int appRegister(Household household) {
        household.setHName(RandomNameUtitls.getRandomJianHan(6));
        household.setPassword(Md5Utils.getMd5(household.getPassword()));
        household.setSourceOfHouseholds("app注册");
        household.setCreatTime(new Date());
        household.setDelFlag(0);
        return householdMapper.addHouseHold(household);
    }

    /**
     * 判断app端账户是否重复
     * @param accountNumber
     * @return
     */
    @Override
    public CommonResult listAccountNumber(String accountNumber) {
        Household household = loginMapper.listAccountNumber(accountNumber);
        if (!StringUtils.isEmpty(household)){
            return CommonResult.ok();
        }
        return CommonResult.fail(household);
    }

    /**
     * app端邮箱是否重复
     * @param email
     * @return
     */
    @Override
    public CommonResult listEmail(String email) {
        Household household = loginMapper.listEmail(email);
        if (!StringUtils.isEmpty(household)){
            return CommonResult.ok();
        }
        return CommonResult.fail(household);
    }

    /**
     * app端手机号是否重复
     * @param phone
     * @return
     */
    @Override
    public CommonResult listPhone(String phone) {
        Household household = loginMapper.listPhone(phone);
        if (!StringUtils.isEmpty(household)){
            return CommonResult.ok();
        }
        return CommonResult.fail(household);
    }

    /**
     * app端邮箱登录
     * @param household
     * @return
     */
    @Override
    public CommonResult emailLogin(Household household) {
        Household household1 = loginMapper.listEmail(household.getEmail());
        if (!StringUtils.isEmpty(household1)){
            if (household.getCode().equals(redisUtil.get("code"))){
                return CommonResult.ok(household1);
            }else{
                return CommonResult.fail("验证码错误!");
            }
        }else {
            return CommonResult.fail("邮箱不存在!");
        }
    }

    /**
     * app端手机号登录
     * @param household
     * @return
     */
    @Override
    public CommonResult phoneLogin(Household household) {
        Household household1 = loginMapper.listPhone(household.getHPhone());
        if (!StringUtils.isEmpty(household1)){
            if (household.getCode().equals(redisUtil.get("code"))){
                return CommonResult.ok(household1);
            }else{
                return CommonResult.fail("验证码错误!");
            }
        }else {
            return CommonResult.fail("手机号不存在!");
        }
    }

    /**
     * 修改密码
     * @param household
     * @return
     */
    @Override
    public int updatePassword(Household household) {
        household.setPassword(Md5Utils.getMd5(household.getPassword()));
        return loginMapper.updatePassword(household);
    }

}
